package com.pll.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pll.controller.admin.order.vo.ConditionPaginationVO;
import com.pll.controller.admin.order.vo.OrderResultVO;
import com.pll.dto.ResponseDto;
import com.pll.mapper.OrderMapper;
import com.pll.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @className: OrderServiceImpl
 * @author: XR_Z
 * @date: 2025/6/9 18:57
 * @Version: 1.0
 * @description: 订单相关service实现类
 */

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    private ResponseDto dto;

    @Override
    public ResponseDto pendingOrders(ConditionPaginationVO conditionPaginationVO) {

        // 1. 构建分页对象
        Page<OrderResultVO> page = new Page<>(conditionPaginationVO.getPageNum(), conditionPaginationVO.getPageSize());


        IPage<OrderResultVO> result = orderMapper.selectPendingOrders(page, conditionPaginationVO);



        return ResponseDto.success(result);

    }

    @Override
    public ResponseDto modifiedToReceived(int id) {
        dto = ResponseDto.err(-1,"请求繁忙~请稍后重试~");

        int i = orderMapper.modifiedToReceived(id);
        if (i > 0) {
            dto = ResponseDto.success(1);
        }
        return dto;
    }

    @Override
    public ResponseDto volumeLawsOrdersTaken(ArrayList<Integer> ids) {
        dto = ResponseDto.err(-1,"请求繁忙~请稍后重试~");

        int i = orderMapper.volumeLawsOrdersTaken(ids);

        if (i == ids.size()) {
            dto = ResponseDto.success(1);
        } else if (i > 0) {
            dto = ResponseDto.fail(0,"部分定单未变更，请检查后重新尝试~");
        }

        return dto;
    }

    @Override
    public ResponseDto refuse(int id) {
        dto = ResponseDto.err(-1,"请求繁忙~请稍后重试~");
        int i = orderMapper.refuse(id);
        if (i > 0) {
            dto = ResponseDto.success(1);
        }
        return dto;
    }

    @Override
    public ResponseDto bulkRejection(ArrayList<Integer> ids) {

        dto = ResponseDto.err(-1,"请求繁忙~请稍后重试~");

        int i = orderMapper.bulkRejection(ids);

        if (i == ids.size()) {
            dto = ResponseDto.success(1);
        } else if (i > 0) {
            dto = ResponseDto.fail(0,"部分定单未变更，请检查后重新尝试~");
        }

        return dto;
    }

}
